<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>欢迎来到陌上的网站</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <h1>欢迎来到陌上的留言板</h1>


        <form action="post_message.php" method="post">
            <textarea name="message" rows="4" cols="50" placeholder="请输入您的留言"></textarea><br>
            <input type="submit" value="提交留言">
        </form>


<h2>留言列表</h2>
<?php

$servername = "localhost";
$username = "陌上";
$password = "123456";
$dbname = "陌上";


$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT id, message, created_at FROM messages ORDER BY created_at DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
        echo "<div class='message'>";
        echo "<p>{$row['message']}</p>";
        echo "<p><small>时间: {$row['created_at']}</small></p>";
        echo "<form action='delete_message.php' method='post'>";
        echo "<input type='hidden' name='id' value='{$row['id']}'>";
        echo "<input type='submit' value='删除'>";
        echo "</form>";
        echo "</div>";
    }
} else {
    echo "暂无留言";
}

$conn->close();
?>

    </div>
</body>
</html>
